package com.t3go.t3_map.business.wait;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shareware.common.ApplicationKt;
import com.shareware.common.listener.Callback;
import com.shareware.common.utils.ScreenExtKt;
import com.t3go.passenger.base.utils.FIvLoader;
import com.t3go.passenger.base.utils.ping.PingManager;
import com.t3go.passenger.t3asset.view.FImageView;
import com.t3go.t3_map.R;
import com.t3go.t3_map.entity.GuideEntity;
import com.t3go.t3_map.entity.Speed;
import com.t3go.t3_map.views.ImageGuideView;
import com.t3go.t3_map.views.T3StrokeTextView;
import com.t3map.api.map.T3Map;
import com.t3map.api.map.model.T3Marker;


public class WaitInfoWindowAdapter implements T3Map.InfoWindowAdapter {
    private final View mInfoWindow;
    private final MapWaitAnimView mWaitAnimView;
    private final TextView mTitleText;

    private final T3StrokeTextView mOriginNameText;
    private final ImageView mCenterIconImage;


    private final View mNetWeakTipsView;

    // 寻找车辆
    private final View mNormalView;

    // 限停提示
    private final View mLimitStopView;
    private final TextView mLimitStopTitleText;
    private final TextView mLimitStopSubTitleText;
    private final ImageGuideView normalGuideView;
    private final ImageGuideView limitGuideView;
    private final FImageView parkView;

    private GuideEntity mGuideEntity;

    public WaitInfoWindowAdapter() {
        mInfoWindow = LayoutInflater.from(ApplicationKt.getContextGlobal()).inflate(R.layout.wait_bubble_positive_time, null);
        mNormalView = mInfoWindow.findViewById(R.id.view_normal);
        mTitleText = mInfoWindow.findViewById(R.id.tv_bubble_positive_time);

        mNetWeakTipsView = mInfoWindow.findViewById(R.id.view_net_tips);
        mCenterIconImage = mInfoWindow.findViewById(R.id.icon_position);
        mWaitAnimView = mInfoWindow.findViewById(R.id.positive_map_wait_anim);

        mLimitStopView = mInfoWindow.findViewById(R.id.view_limit_stop);
        mLimitStopTitleText = mInfoWindow.findViewById(R.id.tv_limit_stop_title);
        mLimitStopSubTitleText = mInfoWindow.findViewById(R.id.tv_limit_stop_sub_title);

        normalGuideView = mInfoWindow.findViewById(R.id.normalGuideView);
        limitGuideView = mInfoWindow.findViewById(R.id.limitGuideView);
        parkView = mInfoWindow.findViewById(R.id.parkView);

        mOriginNameText = mInfoWindow.findViewById(R.id.tv_bubble_info_address);
        mOriginNameText.setStrokeColor(Color.WHITE);
        mOriginNameText.setStrokeWidth(ScreenExtKt.dp2px(ApplicationKt.getContextGlobal(), 3));
    }

    @Override
    public View getInfoWindow(T3Marker marker) {
        return mInfoWindow;
    }

    @Override
    public View getInfoContents(T3Marker marker) {
        return mInfoWindow;
    }

    public void startWaitAnim() {
        if (mWaitAnimView != null) {
            mWaitAnimView.startAnim();
        }
    }

    public void cancelWaitAnim() {
        if (mWaitAnimView != null) {
            mWaitAnimView.cancelAnim();
        }
    }

    public void updateWaitAnimationRate(Speed speed) {
        if (mWaitAnimView != null) {
            mWaitAnimView.startAnimWithRate(speed);
        }
    }

    public void updateBubbleNetStatus(boolean weakNet) {
        checkNetSignal();
    }

    public void updateBubbleGuideData(GuideEntity guideEntity) {
        mGuideEntity = guideEntity;
        if (mLimitStopView != null) {
            showGuideView(mLimitStopView.getVisibility() == View.VISIBLE);
        }
    }

    /**
     * 显示正计时加载动画
     */
    public void showPositiveTimeLoading(String originName) {
        mNetWeakTipsView.setVisibility(View.GONE);
        mOriginNameText.setText(originName);
    }

    /**
     * 更新为特快呼叫图标
     *
     * @param fast 是否特快
     */
    public void updatePositiveIcon(boolean fast) {
        if (fast) {
            FIvLoader.get().load(mCenterIconImage, R.string.f_src_icon_finding_fast);
        } else {
            FIvLoader.get().load(mCenterIconImage, R.string.f_src_icon_finding_normal);
        }
    }

    /**
     * 显示文案
     *
     * @param title        显示文案
     * @param limitMessage 副标题，当副标题不为空时显示限停
     */
    public void showPositiveTime(String title, String limitMessage) {
        if (TextUtils.isEmpty(title)) {
            mNormalView.setVisibility(View.GONE);
            mLimitStopView.setVisibility(View.GONE);
            return;
        }

        if (!TextUtils.isEmpty(limitMessage)) {
            mNormalView.setVisibility(View.GONE);
            mLimitStopView.setVisibility(View.VISIBLE);

            mLimitStopTitleText.setText(title);
            mLimitStopSubTitleText.setText(limitMessage);
        } else {
            mNormalView.setVisibility(View.VISIBLE);
            mLimitStopView.setVisibility(View.GONE);
            mTitleText.setText(title);
        }
        showGuideView(!TextUtils.isEmpty(limitMessage));
        checkNetSignal();
    }


    private void checkNetSignal() {
        boolean signalWeak = !PingManager.getRecentPingReachable();
        if (mNormalView.getVisibility() == View.VISIBLE) {
            mNetWeakTipsView.setVisibility(signalWeak ? View.VISIBLE : View.GONE);
        }

        if (signalWeak) {
//            MapSensorTrack.trackNetMonitor(TrackEvent.EXPOSURE, MapSensorConstants.HomeMap.NET_WEAK_ON_CALL);
        }
    }

    private void showGuideView(boolean limit) {
        if (limit) {
            if (mGuideEntity != null && mGuideEntity.isValid()) {
                limitGuideView.bindData(mGuideEntity, ImageGuideView.Scene.CALLING);
                limitGuideView.setVisibility(View.VISIBLE);
                parkView.setVisibility(View.GONE);
//                SensorAnalyticsAPI.getInstance()
//                        .track(TrackEvent.EXPOSURE, MapSensorConstants.PickUpPassenger.PIN_GUIDE, null);
            } else {
                limitGuideView.setVisibility(View.GONE);
                parkView.setVisibility(View.VISIBLE);
            }
        } else {
            if (mGuideEntity != null && mGuideEntity.isValid()) {
                normalGuideView.bindData(mGuideEntity, ImageGuideView.Scene.CALLING);
                normalGuideView.setVisibility(View.VISIBLE);
//                SensorAnalyticsAPI.getInstance()
//                        .track(TrackEvent.EXPOSURE, MapSensorConstants.PickUpPassenger.PIN_GUIDE, null);
            } else {
                normalGuideView.setVisibility(View.GONE);
            }
        }
    }

    public void setWalkGlideClickListener(Callback<Integer> callback) {
        normalGuideView.setOnClickListener(view -> {
            if (callback != null) {
                callback.call(1);
            }
        });
        limitGuideView.setOnClickListener(view -> {
            if (callback != null) {
                callback.call(2);
            }
        });
    }
}
